//
//  InputBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol InputBillViewDelegate {
    @objc optional func selectOperate(index:Int) //选择操作类型
}

class InputBillView: UIView {
    
    weak var delegate : InputBillViewDelegate?
    
     let numberView = AddNewsView() //银行卡号
    let bankView = SelectNewsView() //银行
    let nameView = AddNewsView() //姓名
    let billDateView = SelectNewsView() //账单日期
    let repaymentView = SelectNewsView() //还款日期
    let cardLinesView = AddNewsView() //信用卡额度
    let billLinesView = AddNewsView() //账单额度
    let addButton = UIButton() //添加

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.mineColor()
        
        let titleLabel = UILabel()
        titleLabel.backgroundColor = UIColor.mineColor()
        titleLabel.text = getBillForInput
        titleLabel.textColor = UIColor.darkGray
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(titleLabel)
        
        numberView.text = afterBankNumber
        numberView.placeholder = bankNumberTips
        numberView.textView.keyboardType = .numberPad
        self.addSubview(numberView)
        
        bankView.title = bankOf
        bankView.text = BankofChina
        bankView.selectBtn.tag = 1
        bankView.selectBtn.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(bankView)
        
        nameView.text = houseHolder
        nameView.placeholder = nameTips
        self.addSubview(nameView)
        
        billDateView.title = billDate
        billDateView.text = KMTool.getCurrentTime()
        billDateView.selectBtn.tag = 2
        billDateView.selectBtn.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(billDateView)
        
        repaymentView.title = repaymentDate
        repaymentView.text = KMTool.getCurrentTime()
        repaymentView.selectBtn.tag = 3
        repaymentView.selectBtn.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(repaymentView)
        
        cardLinesView.text = creditCardLimit
        cardLinesView.placeholder = limitTips
        cardLinesView.textView.keyboardType = .numberPad
        self.addSubview(cardLinesView)
        
        billLinesView.text = billLimit
        billLinesView.placeholder = limitTips
        billLinesView.textView.keyboardType = .numberPad
        self.addSubview(billLinesView)
        
        addButton.setTitle(add_title, for: .normal)
        addButton.backgroundColor = UIColor.red
        addButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        addButton.layer.cornerRadius = RATIO_H(maxNum: 25)
        addButton.layer.masksToBounds = true
        addButton.tag = 4
        addButton.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(addButton)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        numberView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        bankView.snp.makeConstraints { (make) in
            make.top.equalTo(numberView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(numberView)
        }
        
        nameView.snp.makeConstraints { (make) in
            make.top.equalTo(bankView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(bankView)
        }
        
        billDateView.snp.makeConstraints { (make) in
            make.top.equalTo(nameView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(nameView)
        }
        
        repaymentView.snp.makeConstraints { (make) in
            make.top.equalTo(billDateView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(billDateView)
        }
        
        cardLinesView.snp.makeConstraints { (make) in
            make.top.equalTo(repaymentView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(repaymentView)
        }
        
        billLinesView.snp.makeConstraints { (make) in
            make.top.equalTo(cardLinesView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(cardLinesView)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.equalTo(billLinesView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
    }
    
    //MAKR: 选择操作类型
    @objc fileprivate func selectOperate(sender:UIButton) {
        
        self.delegate?.selectOperate!(index: sender.tag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
